<template>
  <div>
    <h1>分类列表</h1>
    <!-- 条件查询 -->
    <el-form :inline="true" :model="searchFrom" class="demo-form-inline">
      <el-form-item label="分类名称">
        <el-input
          v-model="searchFrom.keyWord"
          placeholder="请输入商品分类名称"
          clearable
        />
      </el-form-item>

      <el-form-item label="商品分类状态">
        <el-select
          v-model="searchFrom.typeId"
          style="width: 160px"
          placeholder="请选择分类状态"
          clearable
        >
          <el-option label="显示" :value="0" />
          <el-option label="隐藏" :value="1" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="loadTypeList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button type="success" @click="addParentType">添加</el-button>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <el-table-column prop="id" label="序号" />
      <el-table-column prop="name" label="分类名称" />
      <el-table-column prop="typeImage" label="分类图片">
        <template #default="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="scope.row.typeImage"
          />
        </template>
      </el-table-column>
      <el-table-column prop="typeTop" label="排序" />
      <el-table-column prop="typeStatus" label="状态">
        <template #default="scope">
          <el-tag type="success" v-if="scope.row.typeStatus == 0">显示</el-tag>
          <el-tag type="danger" v-if="scope.row.typeStatus == 1">隐藏</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="success" @click="onAddType(scope.row)"
            >添加</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--添加分类  -->
    <div>
      <el-dialog v-model="dialogFormVisible" title="添加分类" width="500">
        <el-form :model="form" ref="ruleFormRef" :rules="rules">
          <el-form-item
            label="分类名称"
            :label-width="formLabelWidth"
            prop="typeName"
          >
            <el-input v-model="form.typeName" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="分类图片"
            :label-width="formLabelWidth"
            prop="typeImage"
          >
            <el-upload
              class="avatar-uploader"
              :headers="headers"
              action="http://localhost:10010/brand/brand/uploadFile"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <img v-if="form.typeImage" :src="form.typeImage" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
          <el-form-item
            label="排序"
            :label-width="formLabelWidth"
            prop="typeTop"
          >
            <el-input-number v-model="form.typeTop" :min="1" />
          </el-form-item>
          <el-form-item
            label="状态"
            :label-width="formLabelWidth"
            prop="typeStatus"
          >
            <el-radio-group v-model="form.typeStatus">
              <el-radio :value="0" size="large">显示</el-radio>
              <el-radio :value="1" size="large">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              确认
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { getTypeTree, addType } from "@/api/brand";
import { ElMessage } from "element-plus";
import { Plus } from "@element-plus/icons-vue";

import type { UploadProps } from "element-plus";
import { Header } from "element-plus/es/components/table-v2/src/components/index.mjs";
import { fa } from "element-plus/es/locales.mjs";
import type { FormInstance, FormRules } from "element-plus";
const ruleFormRef = ref<FormInstance>();
const tableData = ref([]);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const headers = ref({
  token: sessionStorage.getItem("token"),
});

const form = ref({
  typeImage: null,
  typeName: null,
  typePid: 0,
  typeStatus: null,
  typeTop: null,
});
const rules = ref({
  typeName: [{ required: true, message: "请输入名称", trigger: "blur" }],
  typeImage: [{ required: true, message: "请选择图片", trigger: "blur" }],
  typeTop: [{ required: true, message: "请输入排序", trigger: "blur" }],
  typeStatus: [{ required: true, message: "请选择状态", trigger: "blur" }],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      addType(form.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("添加成功");
          dialogFormVisible.value = false;
          loadTypeList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

// const saveType = () => {
//   addType(form.value).then((res) => {
//     if (res.data.code == 200) {
//       ElMessage.success("添加成功");
//       dialogFormVisible.value = false;
//       loadTypeList();
//     } else {
//       ElMessage.error(res.data.msg);
//     }
//   });
// };

const onAddType = (row: any) => {
  console.log("row", row.id);
  form.value = {
    typeImage: null,
    typeName: null,
    typePid: row.id,
    typeStatus: null,
    typeTop: null,
  };
  dialogFormVisible.value = true;
};
const addParentType = () => {
  form.value = {
    typeImage: null,
    typeName: null,
    typePid: 0,
    typeStatus: null,
    typeTop: null,
  };
  dialogFormVisible.value = true;
};

const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  form.value.typeImage = response.data;
};
const searchFrom = ref({
  keyWord: "",
  typeId: null,
});
const loadTypeList = () => {
  getTypeTree(searchFrom.value).then((res) => {
    tableData.value = res.data.data;
  });
};

onMounted(() => {
  loadTypeList();
});
</script>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
